package edu.guet.server.mapper;

import edu.guet.server.model.Resource;
import edu.guet.server.model.dto.*;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ResourceMapper {
    /**
     * 添加资源
     * @param resource
     */
    @Insert("INSERT into resource(resource.resource_name,resource_url,resource_type) values (#{resource_name},#{resource_url},#{resource_type})")
    @Options(useGeneratedKeys = true, keyProperty = "resource_id", keyColumn = "resource_id")
    void addResource(Resource resource);

    /**
     * 根据resourceId查找resource
     * @param resourceId
     * @return
     */
    @Select("SELECT * from resource where resource_id=#{resourceId}")
    Resource findResourceByCode(Integer resourceId);




    @Update("UPDATE resource SET resource_name = #{newName} WHERE resource_id = #{resourceId}")
    void updateResource(Map<String, Object> params);

    /**
     * 根据学期id写查询相应类型的资源数量
     * @param type
     * @param semester_id
     * @return
     */
    Integer getTypeCountBySemesterId(String type,Integer semester_id);

    /**
     * 将资源和单元相关联
     * @param unit_id
     * @param resource_id
     */
    @Insert("insert into unit_resource(unit_id, resource_id) values (#{unit_id},#{resource_id})")
    void addUnitResource(Integer unit_id, Integer resource_id);

    @Select("SELECT * from resource where resource_id=#{id}")
    Resource findResourceById(Integer id);

    @Insert("INSERT INTO resource_course (resource_id, course_id) values (#{resource_id},#{course_id})")
    void addTechRe(ResourceDTO resourceDTO);

    List<ResourceDTO> showResources(Integer courseId, String type);

    void updateCourseResource(Resource resource);

    @Select("select user_id,resource_id from stu_resource where resource_id=#{resource_id} and user_id=#{user_id} and unit_id=#{unit_id}")
    StuReDTO selectStuRe(StuReDTO stuRe);

    @Insert("INSERT into stu_resource (user_id, resource_id,unit_id) VALUES (#{user_id},#{resource_id},#{unit_id})")
    void addStuResource(StuReDTO stuReDTO);

    @Select("select * from watch_video where (user_id=#{user_id} and unit_id=#{unit_id} and video_id=#{video_id})")
    WatchUnitReDTO selectWatchResource(WatchUnitReDTO watchUnitReDTO);

    @Insert("INSERT INTO watch_video (user_id, unit_id, watch_time) VALUES (#{user_id},#{unit_id},#{watch_time})")
    void addWatchUniRe(WatchUnitReDTO watchUnitReDTO);

    @Update("update watch_video set watch_time=ADDTIME(watch_time, #{watch_time}) where unit_id=#{unit_id} and user_id=#{user_id}")
    void updateWatchRe(WatchUnitReDTO watchUnitReDTO);

    Integer getStuVideoCount(Integer stuId, Integer semesterId);

    String getWatchTime(Integer stuId, Integer semesterId);
    /**
     * 删除
     * @param
     * @return
     */
    @Delete("DELETE FROM resource WHERE resource.resource_id= #{id}")
    int deleteResource(Integer id);

    /**
     * 改名
     * @param id
     * @param name
     */
    @Update("UPDATE resource SET resource_name = #{name} WHERE resource_id = #{id}")
    void changeResourceName(Integer id, String name);


    List<Integer> getReIdsByUnitIds(Integer unitId);

    Integer getCountStuRe(Integer unitId, Integer userId, Integer reId);

    void deleteCourseResource(Integer resourceId);

    List<UnitReList> getUnitstitle(Integer semesterId);

    List<UnitResourceDTO> findReByunitId(Integer unitId);
}
